home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright Cornell University 1986. All rights are reserved.
- */
-
- #include <em.h>
-
- #include <h19.h>
-
-
- extern short usedsdraw; /* set through emprep() */
- extern short emupdwait; /* set through emprep() */
-
-
- /*
- * The h19 emulator routine, which interprets escape commands and other
- * control codes like a Heath-19 (quite similar to DEC VT-52)
- *
- */
-
- h19(thechar)
- char thechar;
- {
- h19str(&thechar, 1);
- }
-
-
- h19str(strp, ccount)
- register unsigned char * strp;
- int ccount;
- {
- register unsigned char * endp = strp + ccount;
- register unsigned char thechar;
- int count;
-
- emprep();
- if (usedsdraw) {
- emdp->inselection = chkinvert();
- setascattr_ds(attrib, emdp->inselection);
-
- }
-
- while (strp < endp) {
- thechar = *strp++; /* & 0x7f -- assumed */
- if (mode == NORMMODE) {
- /* NORMMODE interpreted here to avoid switch overhead */
- /* draw character if alphanumeric */
- if (emdp->emliteral || (thechar >= 0x20 && thechar != 0x7f)) {
- #ifdef BETTERLITERAL
- if (emdp->emliteral) {
- /* remap control chars so they are not interpreted */
- if (thechar == CR) {
- thechar = 0x86;
- }
- else if (thechar == HTAB) {
- thechar = 0x84;
- }
- }
- #endif
- if (inserton) {
- ins_char();
- }
- *charp = thechar;
- *(charp + emdp->screensize) = attrib;
- if (emdp->matchinput) {
- /* we're trying to match input characters */
- if (matchtoken(thechar))
- resumetokens(emdp);
- }
- #ifdef FASTDRAW
- if (usedsdraw) {
- /* we're using direct-to-screen drawing */
- if (modflg) {
- emrefresh(emwindow, TRUE);
- }
- if (emdp->selrectvis) {
- if (emdp->inselection != chkinvert()) {
- if (emdp->inselection)
- emdp->inselection = FALSE;
- else
- emdp->inselection = TRUE;
- setascattr_ds(attrib, emdp->inselection);
- }
- }
- zapchar(thechar);
- clrflg &= ~modmask[ypos]; /* reset clear flag */
- }
- else {
- /* for quickdraw, draw when finished updating map
- if (inserton || ccount == 1)
- ch_draw_qd(thechar);
- else
- */
- modflg |= modmask[ypos];
- }
- #else
- modflg |= modmask[ypos];
- #endif
- /* adjust cursor position */
- if (xpos < lastcol) {
- ++xpos;
- ++charp;
- }
- else if (wrap_around) {
- charp -= xpos;
- xpos = 0;
- if (ypos < emdp->lastrow) {
- charp += linelength;
- ++ypos;
- }
- else {
- (*emdp->scrollup)();
- }
- }
- continue;
- }
- else {
- /* an ascii control character... */
- switch(thechar) {
- case 0x07: {
- /* BELL */
- if (!emdp->emdisable)
- beep();
- break;
- }
- case 0x08: {
- /* BS */
- if (xpos) {
- --xpos;
- --charp;
- }
- break;
- }
- case 0x09: {
- /* HTAB */
- short shift;
-
- if (xpos < lastcol - TABSIZE) {
- shift = ((xpos + TABSIZE) & ~7) - xpos;
- xpos += shift;
- charp += shift;
- }
- else if (xpos < lastcol) {
- ++xpos;
- ++charp;
- }
- break;
- }
- case 0x0c: {
- /* FF */
- if (xpos < lastcol) {
- ++xpos;
- ++charp;
- }
- else {
- charp -= xpos;
- xpos = 0;
- if (ypos < emdp->lastrow) {
- ++ypos;
- charp += linelength;
- }
- else {
- ypos = 0;
- charp = emdp->charr;
- }
- }
- break;
- }
- case 0x0a: {
- /* LF */
- /* NEW draw line if QD? */
- if (ypos < emdp->lastrow) {
- ++ypos;
- charp += linelength;
- }
- else {
- (*emdp->scrollup)();
- }
- break;
- }
- case 0x0d: {
- /* CR */
- charp -= xpos;
- xpos = 0;
- break;
- }
- case 0x1a: {
- /* SUB */
- if (emdp->selrectvis || bkrd_act || emupdwait) {
- /* must redraw through upd */
- modflg = SCRALLMOD;
- }
- else {
- clearrect(&emdp->bigrect);
- clrflg = SCRALLMOD;
- }
- if (!emdp->ibm_keymode)
- savescreen();
-
- c19_blankmap(&emdp->charr[0]);
- xpos = ypos = 0;
- charp = emdp->charr;
- break;
- }
- case 0x1b: {
- /* ESC */
- mode = ESCMODE;
- break;
- }
- case 0x1e: {
- /* RS */
- charp = emdp->charr;
- xpos = ypos = 0;
- break;
- }
- }
- continue;
- }
- }
-
- /* else interpret character according to proper mode */
- if (mode == ESCMODE) {
- switch(thechar) {
- case ESC: {
- /* 2 Escape characters: go into c19 file transfer mode */
- mode = CFTMODE;
- continue;
- }
- case 'A': {
- /* cursor up */
- if (ypos > 0) {
- ypos--;
- charp -= linelength;
- }
- break;
- }
- case 'B': {
- /* cursor down */
- if (ypos < emdp->lastrow) {
- ++ypos;
- charp += linelength;
- }
- break;
- }
- case 'C': {
- /* cursor right */
- if (xpos < lastcol) {
- ++xpos;
- ++charp;
- }
- break;
- }
- case 'D': {
- /* cursor left */
- if (xpos > 0) {
- --xpos;
- --charp;
- }
- break;
- }
- case 'E': {
- /* reset: clear screen */
- if (!emdp->ibm_keymode)
- savescreen();
-
- h19reset();
- #ifdef FASTDRAW
- setascattr_ds(attrib, emdp->inselection);
- #endif
-
- break;
- }
- /* TODO support 7171 modes, invis, highlight, protect */
- case 'U': /* 7171 highlight unprotect */
- case 'W': /* 7171 highlight protect */
- case 'F': {
- attrib |= BOLD;
- #ifdef FASTDRAW
- setascattr_ds(attrib, emdp->inselection);
- #endif
- break;
- }
- case 'S': /* 7171 invisible */
- case 'T': /* 7171 normal unprotect */
- case 'V': /* 7171 normal protect */
- case 'G': {
- /* end alt char set */
- attrib = NORMFONT;
- #ifdef FASTDRAW
- setascattr_ds(attrib, emdp->inselection);
- #endif
- break;
- }
- case 'H': {
- /* home */
- xpos = 0;
- ypos = 0;
- charp = emdp->charr;
- break;
- }
- case 'I': {
- /* reverse index */
- if (ypos == 0)
- ins_lin(0);
- else {
- --ypos;
- charp -= linelength;
- }
- break;
- }
- case 'J': {
- /* clear to end of screen */
- if (emdp->selrectvis || bkrd_act || emupdwait) {
- /* must redraw through upd */
- for (count = ypos; count <= emdp->lastrow; count++)
- modflg |= modmask[count];
- }
- else {
- clr_eol();
- emdp->bigrect.top = (ypos + 1) * emdp->lineheight + emdp->voffset;
- clearrect(&emdp->bigrect);
- emdp->bigrect.top = emdp->voffset;
- }
- if (emdp->logerase || (ypos == 0 && xpos == 0)) {
- /* save the screen before it gets overwritten */
- if (!emdp->ibm_keymode)
- savescreen();
- }
-
- /* blank out the screen map */
- c19_blank(charp, emdp->charrend - charp);
- break;
- }
- case 'K': {
- /* clear EOL */
- clr_eol();
- break;
- }
- case 'L': {
- /* insert line */
- ins_lin(ypos);
- charp -= xpos;
- xpos = 0;
- break;
- }
- case 'M': {
- /* delete line */
- del_lin(ypos);
- charp -= xpos;
- xpos = 0;
- break;
- }
- case 'N': {
- /* delete char */
- del_char();
- break;
- }
- case '@': {
- /* enter insert char mode */
- inserton = TRUE;
- showinsert();
- break;
- }
- case 'O': {
- /* exit insert char mode */
- inserton = FALSE;
- clrinsert();
- break;
- }
- case 'Y': {
- /* cursor calculation */
- mode = YCALC;
- continue;
- }
- case 'Z': {
- /* identify as VT52 */
- vt52response();
- break;
- }
- case '=': {
- /* enter alt keypad mode, vt52 compatibility */
- emdp->vt52altkeypad = TRUE;
- break;
- }
- case '>': {
- /* exit alt keypad mode, vt52 compatibility */
- emdp->vt52altkeypad = FALSE;
- break;
- }
- case '<': {
- if (emdp->termtype == TERM_VT100
- || keydp->termtype == TERM_VT102
- || keydp->termtype == TERM_VT220
- ) {
- /* we've been doing vt52 for vt100, return */
- mode = VT100MODE;
- emdp->em = vt100;
- emdp->emstr = vt100str;
- emend(); /* emprep will be called again */
- vt100str(strp, endp - strp);
- return(0);
- }
- break;
- }
- case 'b': {
- /* clear screen up to cursor position */
- Rect trect;
-
- if (emdp->selrectvis || bkrd_act || emupdwait) {
- /* must redraw through upd */
- for (count = 0; count < ypos; count++)
- modflg |= modmask[count];
- }
- else {
- emdp->bigrect.bottom = ypos * emdp->lineheight + emdp->voffset;
- clearrect(&emdp->bigrect);
- emdp->bigrect.bottom = emdp->bottommarg;
-
- trect.top = ypos * emdp->lineheight + emdp->voffset;
- trect.bottom = trect.top + emdp->lineheight;
- trect.left = emdp->hoffset;
- trect.right = xpos * fontwidth;
- clearrect(&trect);
- }
- #ifdef SAVEONERASE
- if (!emdp->ibm_keymode && emdp->logerase)
- saveline(&emdp->charr[0], charp - emdp->charr + 1);
- /* save the line if needed before it gets overwritten */
- #endif
- c19_blank(&emdp->charr[0], charp - emdp->charr + 1);
- break;
- }
- case 'l': {
- /* clear current line */
- Rect temprect;
- char * destp;
-
- if (emdp->selrectvis || bkrd_act || emupdwait) {
- /* must redraw through upd */
- modflg |= modmask[ypos];
- }
- else {
- temprect.top = ypos * emdp->lineheight + emdp->voffset;
- temprect.left = emdp->hoffset;
- temprect.bottom = temprect.top + emdp->lineheight;
- temprect.right = emdp->rightmarg;
- clearrect(&temprect);
- }
- destp = emdp->charr + ypos * linelength;
- if (!emdp->ibm_keymode)
- saveline(destp, linelength);
-
- c19_blank(destp, linelength);
- break;
- }
- case 'o': {
- clrtocurs();
- break;
- }
- case 'p': {
- /* ordinarily produces inverse video */
- #ifdef INVEQALT
- /* start alt char set */
- attrib |= BOLD;
- #else
- /* reverse video */
- attrib |= REVERSE;
- #endif
- #ifdef FASTDRAW
- setascattr_ds(attrib, emdp->inselection);
- #endif
- break;
- }
- case 'q': {
- /* end alt char set */
- attrib = NORMFONT;
- #ifdef FASTDRAW
- setascattr_ds(attrib, emdp->inselection);
- #endif
- break;
- }
- #ifdef UNDERSTOOD
- /* TODO check out what this mode is supposed to do */
- case 't': {
- /* enter shift keypad mode */
- emdp->vt52shiftkeypad = TRUE;
- break;
- }
- case 'u': {
- /* exit shift keypad mode */
- emdp->vt52shiftkeypad = FALSE;
- break;
- }
- #endif
- case 'v': {
- /* wrap on */
- wrap_around = TRUE;
- break;
- }
- case 'w': {
- /* wrap off */
- wrap_around = FALSE;
- break;
- }
- case 'x': {
- /* set heath modes */
- mode = SETMODES;
- continue;
- }
- case 'y': {
- /* reset heath modes */
- mode = RESETMODES;
- continue;
- }
- case '+': {
- /* go into IBM keymode */
- if (!emdp->ibm_keymode) {
- emdp->ibm_keymode = TRUE;
- setibm_keymode(emdp->ibm_keymode);
- }
- break;
- }
- case '-': {
- /* exit IBM keymode */
- if (emdp->ibm_keymode) {
- emdp->ibm_keymode = FALSE;
- setibm_keymode(emdp->ibm_keymode);
- }
- break;
- }
- }
- mode = NORMMODE;
- }
- else if (mode == SETMODES) {
- switch (thechar) {
- case '4': {
- showinsert();
- break;
- }
- }
- mode = NORMMODE;
- }
- else if (mode == RESETMODES) {
- switch (thechar) {
- case '4': {
- clrinsert();
- break;
- }
- }
- mode = NORMMODE;
- }
- else if (mode == YCALC) {
- emdp->argarr[0] = thechar - ' ';
- mode = XCALC;
- }
- else if (mode == XCALC) {
- short ex;
- short ey = emdp->argarr[0];
-
- ex = thechar - ' ';
- mode = NORMMODE;
- if (ey >= 0 && ey <= emdp->lastrow && ex >= 0 && ex <= lastcol) {
- ypos = ey;
- xpos = ex;
- charp = emdp->charr + ((ypos * linelength) + xpos);
- }
- }
- else if (mode == CFTMODE) {
- cft(thechar);
- }
- else if (mode == CFT2MODE) {
- cft2(thechar);
- }
- else if (mode == C19FTMODE) {
- c19ft(thechar);
- }
- }
- newcursor();
- emend();
- }
-
-
- /* reset the h19 emulator */
-
- h19reset()
- {
- GrafPtr oport;
-
- emreset();
-
- /* blank out the screen map */
- c19_blankmap(&emdp->charr[0]);
-
- setcontext(emdp); /* update globals */
-
- if (emdp->emwindow) {
- if (updatewait()) {
- modflg = emdp->modflg = SCRALLMOD;
- /* update emdp since this routine may not be
- called from screen_service */
- }
- else {
- GetPort(&oport);
- SetPort(emwindow);
-
- clearrect(&emdp->bigrect);
-
- clrflg = emdp->clrflg = SCRALLMOD;
- /* update emdp since this routine may not be
- called from screen_service */
-
- emdp->curson = FALSE;
- if (emdp == keydp)
- trackon = FALSE;
-
- if (emdp->selrectset)
- selinvert(emdp);
-
- SetPort(oport);
- }
- }
- newcursor();
- }
-
-
- emreset()
- {
- emdp->mode = NORMMODE; /* interpretation mode of emulator */
-
- #ifdef VARVTSIZE
- if (emdp->charr)
- DisposPtr(emdp->charr);
- if (emdp->tabset)
- DisposPtr(emdp->tabset);
-
- emdp->charr = NewPtr((Size) (2 * emdp->linelength * emdp->linecount));
- emdp->tabset = NewPtr((Size) emdp->linelength);
- #endif
-
- #ifdef VARVTSIZE
- emdp->lastcol = emdp->linelength - 1;
- emdp->lastrow = emdp->linecount - 1;
-
- emdp->screensize = emdp->linelength * emdp->linecount;
- emdp->size23 = emdp->screensize - emdp->linelength;
- #else
- emdp->lastcol = LASTCOL;
- emdp->lastrow = LASTROW;
- emdp->linelength = LINELENGTH;
- emdp->screensize = SCREENSIZE;
-
- emdp->size23 = SIZE23;
- emdp->linecount = 24;
- #endif
-
- emdp->xpos = 0;
- emdp->ypos = 0;
- emdp->charp = emdp->charr; /* cursor loc for these arrays */
- emdp->charrend = &emdp->charr[emdp->screensize];
- emdp->atarr = emdp->charp + emdp->screensize; /* attributes on screen */
- emdp->secondline = emdp->charp + emdp->linelength;
- emdp->lastline = emdp->charp + emdp->size23;
- emdp->scrolltop = TOPROW;
- emdp->scrollbottom = emdp->lastrow;
-
- emdp->attrib = 0;
- emdp->charset = 0;
-
- emdp->bigrect.top = emdp->voffset;
- emdp->bigrect.left = emdp->hoffset;
- emdp->bigrect.bottom = emdp->bottommarg;
- emdp->bigrect.right = emdp->rightmarg;
- }
-
-
- /* respond to terminal type inquiry: we are a vt52! */
-
- vt52response()
- {
- (*emdp->sendchar)(ESC);
- (*emdp->sendchar)('/');
- (*emdp->sendchar)('K');
- (*emdp->putflush)();
- }
-
-
- /* blank out ccount char/attribs */
-
- c19_blank(charp, ccount)
- register unsigned char * charp;
- register int ccount;
- {
- register unsigned char * attp = charp + emdp->screensize;
-
- while (ccount--) {
- *charp++ = ' ';
- *attp++ = 0;
- }
- }
-
-
- /* blank out an h19/vt100 screen */
-
- c19_blankmap(charp)
- register unsigned char * charp;
- {
- register unsigned char * attp = charp + emdp->screensize;
- register int ccount = emdp->screensize;
-
- while (ccount--) {
- *charp++ = ' ';
- *attp++ = 0;
- }
- }
-
-